package com.zttek.photosShareProject.modules.web.manager;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.modules.model.Notice;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.share.IInfoService;
import com.zttek.photosShareProject.modules.service.share.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 13296 on 2017/9/19.
 */
@Controller
public class SystemInfoController extends MyBaseController{
    @Autowired
    private IInfoService infoService;
    @Autowired
    private INoticeService noticeService;
    /**
     * 系统通知列表
     * @param pn
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "admin/info",method = RequestMethod.GET)
    public String infoView(int pn, Model model, String searchValue, HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        List<Notice> list = null;
        Page<Notice> page = new Page<>(pn,10);
        try {
            page = noticeService.selectLikeNoticePage(page,searchValue);
        }catch (NullPointerException e){
            try {
                page = noticeService.selectAllNoticePage(page);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            list = page.getRecords();
            model.addAttribute("systemInfoList",list);
            model.addAttribute("pages",page.getPages());
            model.addAttribute("pn",pn);
            model.addAttribute("searchValue",searchValue);
        }
        return null;
    }

    @RequestMapping(value = "admin/info",method = RequestMethod.POST)
    public String info(int pn, Model model, String searchValue,HttpServletRequest request){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        infoView(pn,model,searchValue,request);
        return null;
    }

    @RequestMapping(value = "admin/addSystemInfo",method = RequestMethod.GET)
    public String addSystemInfoView(@RequestParam("noticeId") Integer id,Model model,HttpServletRequest request ){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        if(id!=null){
            try {
                Notice notice = noticeService.selectNotice(id);
                model.addAttribute("content",notice.getPage());
                model.addAttribute("isPublish",notice.getPublishTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @RequestMapping(value = "admin/addSystemInfo",method = RequestMethod.POST)
    public String addSystemInfo(Model model){
        return null;
    }

    /**
     * 保存公告
     * @param content
     * @param publish
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "admin/saveSystemInfo",method = RequestMethod.POST)
    public Object saveSystemInfo(String content,boolean publish,Model model){
        System.out.println("content:"+content);
        System.out.println("publish:"+publish);
        try {
            noticeService.newNotice(content,publish);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess();
    }

    /**
     * 删除系统公告
     * @param id
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "admin/deleteSystemInfo",method = RequestMethod.POST)
    public Object deleteSystemInfo(@RequestParam("noticeId") Integer id, Model model){
        try {
            noticeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess();
    }

    /**
     * 发布系统公告
     * @param id
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "admin/publishSystemInfo",method = RequestMethod.POST)
    public Object publishSystemInfo(@RequestParam("noticeId") Integer id, Model model){
        try {
            noticeService.publish(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderSuccess();
    }


    @RequestMapping(value = "admin/editSystemInfo",method = RequestMethod.GET)
    public String editSystemInfoView(Model model,HttpServletRequest request ){
        if(request.getSession().getAttribute("currentAdmin")==null){
            return "redirect:/adminLogin";
        }
        return null;
    }

}
